// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using System.Ext;
using LABSMFController.LABS.Classses;

namespace LABSMFController.LABS.Services
{
    public class QueueServiceClass : BeerService
    {
        public readonly static int MAXQueueLength = 10;

        private static Queue[] _queues;

        private static object _locker;

        public override void Configure()
        {
            _queues = new Queue[MAXQueueLength];
            _locker = new object();
            Console.Write("QueueService configured");
        }

        public override void Start()
        {
            Console.Write("QueueService start");
        }

        public override void Stop()
        {
            Console.Write("QueueService stop");
        }

        public string GetLocationClientOutPut()
        {
            string ret = "HEADER:Queue:RequestID,CallingClass,Message;";
            for (int c = 0; c < MAXQueueLength; c++)
            {
                if (_queues[c] != null && ! _queues[c].IsAnswered)
                    ret = ret + "\r\n" + _queues[c].RequestID + "," + _queues[c].CallingClass + "," + _queues[c].Message +
                          ";";
            }
            return ret;
        }

        public int AddToQ(string callingClass, string message, int responseType)
        {
            lock (_locker)
            {
                for (int c = 0; c < MAXQueueLength; c++)
                {
                    if (_queues[c] != null) continue;

                    _queues[c] = new Queue
                                     {
                                         CallingClass = callingClass,
                                         Message = QueueResponseType.GetResponseName(responseType) + " : " + message,
                                         RequestID = c,
                                         ResponseType = responseType
                                     };
                    return c;
                }
            }
            return -1;
        }

        public object GetAnswer(int requestID)
        {
            lock (_locker)
            {
                if (requestID < 0 || requestID > MAXQueueLength - 1) return null;

                if (_queues[requestID] != null)
                    if (_queues[requestID].IsAnswered)
                    {
                        var ret = _queues[requestID].Answer;
                        _queues[requestID] = null;
                        return ret;
                    }
            }
            return null;
        }

        public void Answer(int requestID, string response)
        {
            lock (_locker)
            {
                if (requestID < 0 || requestID > MAXQueueLength - 1) return;

                if (_queues[requestID] != null)
                {
                    _queues[requestID].Answer = response;
                }
            }
            return;
        }
    }
}